package browserUI
{
	import dev.touchswipe.shared.Fonts;
	
	import flash.display.CapsStyle;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Tab extends Sprite
	{
		
		private var _titleText:TextField;
		private var _closeBtn:Sprite;
		
		public function Tab(id:int)
		{
			_titleText=new TextField();
			_titleText.selectable=false;
			_titleText.embedFonts=true;
			_titleText.defaultTextFormat=new TextFormat(Fonts.SegoeUIL,15);
			addChild(_titleText);
			
			_closeBtn=new Sprite();
			_closeBtn.x=180;
			_closeBtn.y=15;
			_closeBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{
				dispatchEvent(new UICommandEvent(UICommandEvent.CLOSE_TAB,id));
			});
			addChild(_closeBtn);
			
			addEventListener(MouseEvent.CLICK,function (event:MouseEvent):void{
				if(event.target!=_closeBtn)
					dispatchEvent(new UICommandEvent(UICommandEvent.SELECT_TAB,id));
			});
			
			redraw();
		}
		
		public var id:int;
		
		private var _active:Boolean;
		
		public function setTitleText(text:String):void{
			_titleText.text=text;
		}
		
		public function get active():Boolean
		{
			return _active;
		}
		
		public function set active(value:Boolean):void
		{
			_active = value;
			redraw();
		}

		
		private var _width:Number=500;

		public override function get width():Number
		{
			return _width;
		}

		public override function set width(value:Number):void
		{
			_width = value;
			redraw();
		}
		private function redraw():void{
			graphics.beginFill(_active?0x99CCFF:0xE7E6EB);
			graphics.drawRoundRectComplex(5,0,190,30,15,15,0,0);
			
			_titleText.width=152;
			_titleText.height=30;
			_titleText.x=10;
			
			_closeBtn.graphics.clear();
			_closeBtn.graphics.beginFill(_active?0x27A0C1:0xA7A9AC);
			_closeBtn.graphics.drawCircle(0,0,13);
			_closeBtn.graphics.endFill();
			_closeBtn.graphics.lineStyle(4,_active?0x99CCFF:0xE7E6EB,1,false,"normal",CapsStyle.SQUARE);
			_closeBtn.graphics.moveTo(-4,-4);
			_closeBtn.graphics.lineTo(4,4);
			_closeBtn.graphics.moveTo(-4,4);
			_closeBtn.graphics.lineTo(4,-4);
		}

	}
}